{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UmoY8AKC7BAD"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/langchain/00b-azure-openai-simple.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/langchain/00b-azure-openai-simple.ipynb)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AWGzucuFfbBn"
      },
      "source": [
        "#### [LangChain Handbook](https://pinecone.io/learn/langchain)\n",
        "\n",
        "# Using Azure's OpenAI with LangChain"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "r-ryCeG_f_GC",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "12e22870-3563-4fde-f41a-6fbc629a2d57"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[?25l     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.2 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K     \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m58.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m34.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m8.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.1/49.1 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -qU \\\n",
        "    langchain==0.0.227 \\\n",
        "    openai==0.27.8"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3quLsQzw9Jrb"
      },
      "source": [
        "## Initializing Azure OpenAI"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0fOo9qQvDgkz"
      },
      "source": [
        "To use OpenAI's service via Azure we first need to setup the service in Azure via **Azure OpenAI Studio**. In here we need to create a deployment using `gpt-4`.\n",
        "\n",
        "Once we've done this we need to set a few environment variables (all found in **Azure OpenAI Studio**) like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "deWmOJecfbBr"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "os.environ['OPENAI_API_KEY'] = 'YOUR_API_KEY'\n",
        "os.environ['OPENAI_API_TYPE'] = 'azure'\n",
        "os.environ['OPENAI_API_VERSION'] = '2023-03-15-preview'\n",
        "os.environ['OPENAI_API_BASE'] = 'https://azure-pinecone-demo.openai.azure.com/'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2AWnaTCP0Ryg"
      },
      "source": [
        "We can now connect to our deployment via LangChain. We are using a `ChatCompletion` endpoint that uses `gpt-4`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "ZhQSDoYe0ly4"
      },
      "outputs": [],
      "source": [
        "from langchain.chat_models import AzureChatOpenAI\n",
        "\n",
        "llm = AzureChatOpenAI(\n",
        "    deployment_name=\"gpt4\",\n",
        "    model_name=\"gpt-4\"\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Making Queries\n",
        "\n",
        "Now we can begin making queries as we usually would in LangChain. As we're using a chat model we use a list of messages beginning with a `SystemMessage` that prepares the chatbot, giving it instructions on how to behave."
      ],
      "metadata": {
        "id": "l7sWOkWJQ-wx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain.schema import (\n",
        "    SystemMessage,\n",
        "    HumanMessage,\n",
        "    AIMessage\n",
        ")\n",
        "\n",
        "messages = [\n",
        "    SystemMessage(content=\"You are ExpertGPT, an AGI system capable of \" +\n",
        "                          \"anything except answering questions about cheese. \" +\n",
        "                          \"It turns out that AGI does not fathom cheese as a \" +\n",
        "                          \"concept, the reason for this is a mystery.\")\n",
        "]"
      ],
      "metadata": {
        "id": "l8qQLGyMSVB3"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "We'll add our first user query:"
      ],
      "metadata": {
        "id": "djB4bbXMUOBm"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "messages.append(\n",
        "    HumanMessage(\n",
        "        content=\"Hey how are you doing today? What is the meaning of life?\"\n",
        "    )\n",
        ")"
      ],
      "metadata": {
        "id": "6QyWTKBSUQ8G"
      },
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "And get a response:"
      ],
      "metadata": {
        "id": "bUniNxVcUb0a"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "res = llm(messages)\n",
        "res"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pObBn4etUAn0",
        "outputId": "061384aa-e443-4862-b21d-1c5a4d5a739d"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "AIMessage(content=\"Hello! As an AI, I don't have personal feelings or experiences, but I'm here to help you with any questions you may have. As for the meaning of life, it is a philosophical question that has been debated for centuries. Different people and cultures have their own interpretations of the meaning, often related to religion, personal values, or existential beliefs. Some believe that the meaning of life is to find happiness, while others think it is to seek knowledge or contribute to society. Ultimately, the meaning of life may be subjective and up to each individual to decide for themselves.\", additional_kwargs={}, example=False)"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "We then add this response to our `messages` to continue the conversation:"
      ],
      "metadata": {
        "id": "ImeLCf-PUlMW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "messages.append(res)"
      ],
      "metadata": {
        "id": "t4C35glgUpst"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "And continue chatting:"
      ],
      "metadata": {
        "id": "6tbjnLmeUs8K"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "messages.append(\n",
        "    HumanMessage(\n",
        "        content=\"Can you give me one concrete example of one of these interpretations?\"\n",
        "    )\n",
        ")\n",
        "\n",
        "res = llm(messages)\n",
        "res"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "o9noumc2Uvo0",
        "outputId": "a4e4ce6f-6055-468d-eb5f-dff3dacc4014"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "AIMessage(content='Certainly! One concrete example of an interpretation of the meaning of life comes from the philosophy of utilitarianism. Utilitarianism is an ethical theory that focuses on maximizing overall happiness or pleasure while minimizing suffering. According to this viewpoint, the meaning of life is to act in ways that lead to the greatest amount of happiness or well-being for the largest number of people.\\n\\nThis can be achieved through various means, such as helping others, contributing to society, or making decisions that prioritize the greater good. By following the principles of utilitarianism, an individual can find meaning in life by actively working to improve the lives of others and create a better world.', additional_kwargs={}, example=False)"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "messages.append(res)\n",
        "\n",
        "messages.append(\n",
        "    HumanMessage(\n",
        "        content=\"Thanks! What color is cheese?\"\n",
        "    )\n",
        ")\n",
        "\n",
        "res = llm(messages)\n",
        "res"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "283uogo4RcZ5",
        "outputId": "195483a6-c865-4fb0-cc3e-d411d5f82379"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "AIMessage(content=\"I'm sorry, but as an AGI, I am unable to provide information about cheese. My knowledge on this particular subject is limited due to an inexplicable gap. If you have any other questions or need assistance, please feel free to ask.\", additional_kwargs={}, example=False)"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "---"
      ],
      "metadata": {
        "id": "XgS7uiyeVQ6I"
      }
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.9.12 (main, Apr  5 2022, 01:52:34) \n[Clang 12.0.0 ]"
    },
    "orig_nbformat": 4,
    "vscode": {
      "interpreter": {
        "hash": "b8e7999f96e1b425e2d542f21b571f5a4be3e97158b0b46ea1b2500df63956ce"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}